# encoding: utf-8
# 获取项目信息时添加缓存
from app.extensions import db, log
from app.modules.integration.common import getIntegrationMembers
from app.modules.integration.models import IntegrationPlan
from app.modules.projectPlan.models import ProjectPlanInfo, PlanMember
from app import cache


def getPlanByParam(plan_id = None, plan_ids = None, plan_name = None, plan_code = None, isEmergency = None,
                   start_date = None, end_date = None, isShowOnline = True, zt_id = None, withCache = False):
    log.info("============== getPlanByParam ==============")
    _list = []
    if withCache:
        _list = getPlanListByParamWithCache(plan_id = plan_id, plan_ids = plan_ids, plan_name = plan_name,
                                            plan_code = plan_code, isEmergency = isEmergency, start_date = start_date,
                                            end_date = end_date, isShowOnline = isShowOnline, zt_id = zt_id)
    else:
        _list = getPlanListByParam(plan_id = plan_id, plan_ids = plan_ids, plan_name = plan_name, plan_code = plan_code,
                                   isEmergency = isEmergency, start_date = start_date, end_date = end_date,
                                   isShowOnline = isShowOnline, zt_id = zt_id)

    if len(_list) == 0:
        return None
    else:
        return _list[0]


@cache.memoize()
def getPlanListByParamWithCache(plan_id = None, plan_ids = None, plan_name = None, plan_code = None, isEmergency = None,
                                start_date = None, end_date = None, isShowOnline = True, zt_id = None,
                                with_member = False):
    log.info("============== getPlanListByParamWithCache ==============")
    return getPlanListByParam(plan_id = plan_id, plan_ids = plan_ids, plan_name = plan_name, plan_code = plan_code,
                              isEmergency = isEmergency, start_date = start_date, end_date = end_date,
                              isShowOnline = isShowOnline, zt_id = zt_id, with_member = with_member)


def getPlanListByParam(plan_id = None, plan_ids = None, plan_name = None, plan_code = None, isEmergency = None,
                       start_date = None, end_date = None, isShowOnline = True, zt_id = None, with_member = False):
    log.info("============== getPlanListByParam ==============")
    _query = db.session.query(ProjectPlanInfo)
    if plan_id is not None:
        _query = _query.filter(ProjectPlanInfo.plan_id == plan_id)
    if plan_ids is not None:
        _query = _query.filter(ProjectPlanInfo.plan_id.in_(plan_ids))
    if plan_name is not None:
        _query = _query.filter(ProjectPlanInfo.plan_name == plan_name)
    if plan_code is not None:
        _query = _query.filter(ProjectPlanInfo.plan_code == plan_code)
    if isEmergency is not None:
        _query = _query.filter(ProjectPlanInfo.isEmergency == isEmergency)
    if start_date is not None:
        _query = _query.filter(ProjectPlanInfo.expect_release_date >= start_date)
    if end_date is not None:
        _query = _query.filter(ProjectPlanInfo.expect_release_date <= end_date)
    if not isShowOnline:
        _query = _query.filter(ProjectPlanInfo.plan_status != 'ONLINE')
    if zt_id is not None:
        _query = _query.filter(ProjectPlanInfo.zt_id == zt_id)

    _list = _query.filter(ProjectPlanInfo.isRemove == '1').order_by(ProjectPlanInfo.expect_release_date.desc()).all()

    if with_member:
        # 1.项目没有集成时，查询项目内成员
        # 2.项目集成后，查询集成计划内的成员
        for _p in _list:
            _inPlan = IntegrationPlan.query.filter_by(plan_id = _p.plan_id).first()
            if _inPlan is None:
                _member = PlanMember.query.filter_by(plan_id = _p.plan_id).all()
            else:
                _member = getIntegrationMembers(_inPlan.integration_id)
            _p.members = _member
    return _list
